import java.util.concurrent.Executors;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class Conti_cons {

	
	
	 static String url ;
	 static String subject;
	 static MessageConsumer consumer ;
	 static Destination destination;
	 static boolean getnewmessage = true;
	 	
    public static void main(String[] args) throws JMSException {
	
	
	
	intitaors();
	
    	Executors.newSingleThreadExecutor().execute(new Runnable(){
   
    	    public void run(){
    	        while(true){
    	            // your code here
    	        	
    	        	try {
    	       
					if(	getnewmessage==true)
						{getnewmessage=false;
						messagecheaker();}
					
					} catch (JMSException e) {
					
						e.printStackTrace();
					}
    	        	
    	        }
    	    }
    	});
    
    
    	
    	
    	

    	Executors.newSingleThreadExecutor().execute(new Runnable(){
   
    	    public void run(){
    	        while(true){
    	            // your code here
    	        	
    	        	try {
    	        		getnewmessage=true;
    	        		Thread.sleep(1);
    	        		
					
					} catch (InterruptedException e) {
					
						e.printStackTrace();
					}
    	        	
    	        }
    	    }
    	});
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    	
    }

	private static void intitaors() throws JMSException {
		
		  url = ActiveMQConnection.DEFAULT_BROKER_URL;

			   
			    subject = "TESTQUEUE";
			
			
			  ConnectionFactory connectionFactory
	          = new ActiveMQConnectionFactory(url);
	      Connection connection = connectionFactory.createConnection();
	      connection.start();

	      // Creating session for seding messages
	      Session session = connection.createSession(false,
	          Session.AUTO_ACKNOWLEDGE);

	      // Getting the queue 'TESTQUEUE'
	       destination = session.createQueue(subject);

	      // MessageConsumer is used for receiving (consuming) messages
	        consumer = session.createConsumer(destination);
		
	}

	protected static void messagecheaker() throws JMSException {
		
		
	

      // Here we receive the message.
      // By default this call is blocking, which means it will wait
      // for a message to arrive on the queue.
      Message message = consumer.receive();

      // There are many types of Message and TextMessage
      // is just one of them. Producer sent us a TextMessage
      // so we must cast to it to get access to its .getText()
      // method.
      
      
      
      
      if (message instanceof TextMessage) {
          TextMessage textMessage = (TextMessage) message;
          System.out.println("Received message "+ textMessage.getText() );
      }
       
    //  connection.close();
   	
		
		
		
		
		
		
		
		
		
		
		
	}
    
    
}
